<?php

namespace app\coupons\controller;

use library\Controller;
use think\Db;


/** 优惠券
 * Created by PhpStorm.
 * User: ycp
 * Date: 2019/11/18
 * Time: 21:07
 */

class CardRecord extends Controller
{
    protected $table = 'lite_member_amount_record';
    protected $memberTable = 'store_member';
    protected $cardTable = 'lite_card';

    //优惠券首页
    public function index()
    {
        $this->title = '充值记录';
        $where = ['is_deleted' => '0'];
        $this->statusTitle = ['1' => '充值', '0' => '使用'];
        $this->_query($this->table)->like('name')->where($where)->order('id', 'desc')->page();
    }

    /**
     * 数据列表处理
     * @param array $data
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _index_page_filter(&$data)
    {
        $userIds = array_unique(array_column($data, 'open_id'));
        $memberList = db($this->memberTable)->whereIn('openid', $userIds)->select();

        foreach ($data as &$vo) {
            foreach ($memberList as $member) if ($member['openid'] === $vo['open_id']) {
                $member['nickname'] = urldecode(emoji_decode($member['nickname']));
                $vo['member'] = $member;
            }

        }

    }


    //添加优惠券
    public function add()
    {
        $this->title = '添加优惠券';
        $this->isAddMode = '1';
        $this->typeData = config('data.coupons_type');
        $this->useTypeData = config('data.useTypeData');
        $this->limitData = config('data.limitCouponsType');
        $this->activityType = config('data.activityType');
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    public function edit()
    {
        $this->title = '编辑优惠券';
        $this->typeData = config('data.coupons_type');
        $this->useTypeData = config('data.useTypeData');
        $this->limitData = config('data.limitCouponsType');
        $this->activityType = config('data.activityType');
        $this->isAddMode = '0';
        $this->applyCsrfToken();
        return $this->_form($this->table, 'form');
    }

    /**
     * 表单结果处理
     * @param boolean $result
     */
    protected function _form_result($result)
    {
        if ($result && $this->request->isPost()) {
            if($this->isAddMode == 1){

            }else{

            }
            $this->success('优惠券编辑成功！', 'javascript:history.back()');
        }
    }

    public function del()
    {
        $this->applyCsrfToken();
        $this->_delete($this->table);
    }


    public function _form_filter(&$data)
    {
        if ($this->request->isGet()) {
            if($data){
                $data['use_start_time'] = substr($data['use_start_time'], 0, 10);
                $data['use_end_time'] = substr($data['use_end_time'], 0, 10);
            }
        } elseif ($this->request->isPost()) {
            $data['create_time'] = date('Y-m-d H:i:s');
            if(empty($data['use_start_time'])){
                unset($data['use_start_time']);
            }
            if(empty($data['use_end_time'])){
                unset($data['use_end_time']);
            }
            $data['status'] = 2;
            $data['coupons_code'] = $this->createCouponsCode();
            $data['user_ids'] = str_replace('，', ',', $data['user_ids']);
        }
    }

    public function showIndex()
    {
        $this->_save($this->table, ['is_show_index' => $this->request->param('status')]);
    }

    private function createCouponsCode(){
        return date('Ymd').substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
    }



}